package com.example.simplecardtenantbank.service.impl;

import com.example.simplecardtenantbank.DTO.CreditListDTO;
import com.example.simplecardtenantbank.DTO.TBankBillDTO;
import com.example.simplecardtenantbank.Utils.TimeHelpUtil;
import com.example.simplecardtenantbank.dataobject.TBankbill;
import com.example.simplecardtenantbank.dataobject.TCard;
import com.example.simplecardtenantbank.form.BankBillForm;
import com.example.simplecardtenantbank.repository.TBankbillRepository;
import com.example.simplecardtenantbank.repository.TBankbillShoppingsheetRepository;
import com.example.simplecardtenantbank.repository.TCardRepository;
import com.example.simplecardtenantbank.service.CreditCardService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CreditCardServiceImpl implements CreditCardService{

    @SuppressWarnings("all")
    @Autowired
    private TBankbillShoppingsheetRepository tBankbillShoppingsheetRepository;

    @SuppressWarnings("all")
    @Autowired
    private TBankbillRepository tBankbillRepository;

    @SuppressWarnings("all")
    @Autowired
    private TCardRepository tCardRepository;

    @Override
    public List<CreditListDTO> findByUserId(String userId) {
        List<TCard> tCardList = tCardRepository.findByUserId(userId);
        List<CreditListDTO> creditListDTOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(tCardList)){
            for (TCard tCard : tCardList) {
                CreditListDTO creditListDTO = new CreditListDTO();
                BeanUtils.copyProperties(tCard, creditListDTO);
                String cardId = creditListDTO.getCardId();
                TBankbill tBankbill = tBankbillRepository.findByCardId(cardId);
                if(!StringUtils.isEmpty(tBankbill.getCardId())) {
                    creditListDTO.setPaymentDueDate(tBankbill.getPaymentDueDate());
                    creditListDTO.setCardNums(tBankbill.getCardNums());
                    String time = TimeHelpUtil.date2TimeStamp(creditListDTO.getPaymentDueDate(),"yyyy-MM-dd");
                    Long now = new Date().getTime()/1000;
                    Long resultTime = Long.valueOf(time).longValue() - now;
                    Long resetDays = resultTime/86400;
                    creditListDTO.setRestDays(resetDays);
                }

                //获取数据库数据条数
                Integer limit  = tBankbillRepository.getCountByCardId(cardId);
                if(limit >= 12 ){
                    limit = 12;
                }
                BankBillForm bankBillForm = new BankBillForm();
                bankBillForm.setCardId(cardId);
                bankBillForm.setNum(limit);
                List<TBankbill> tBankbillList = tBankbillRepository.findByCardIds(bankBillForm);
                List<TBankBillDTO> tBankBillDTOList = new ArrayList<>();
                for(TBankbill tBankbill1 : tBankbillList){
                    TBankBillDTO tBankBillDTO = new TBankBillDTO();
                    BeanUtils.copyProperties(tBankbill1, tBankBillDTO);
                    tBankBillDTOList.add(tBankBillDTO);
                }
                creditListDTO.setTBankBillDTOList(tBankBillDTOList);
                creditListDTOList.add(creditListDTO);
            }
        }
        return creditListDTOList;
    }
}
